home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Util / Pm-Pz / Protectomat v2.6.cpt / protectomat.2.6 / pmLoad.c < prev    next >
Text File  |  1991-10-14  |  8KB  |  351 lines

  1. /*
  2.  * appleshare protection restore program by
  3.  * Aaron Wohl (aw0g+@andrew.cmu.edu)
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <time.h>
  11.  
  12. #define VER_MAJ (2)
  13. #define VER_MIN (6)
  14. #define NIL 0L
  15. extern void exit(int);
  16. FILE *infile;
  17. #define BOGUS_ID (-1)
  18. #define BOGUS_PROTECTION (-1)
  19.  
  20. #define LOG_NAME "protectoload.log"
  21.  
  22. #define EEXTERN
  23. #include "pmCommon.h"
  24.  
  25. long num_warnings=0;
  26. FILE *outfile;
  27.  
  28. /*
  29.  * str_fname - convert a file name into a unix string
  30.  */
  31. char *str_fname(char *fin);
  32. char *str_fname(fin)
  33. char *fin;
  34. {
  35.     *(fin+1+((*fin)))=0;
  36.     return fin+1;
  37. }
  38.  
  39. /*
  40.  * map a group or username to a number
  41.  */
  42. long map_name_to_num(cache_entry_pt acache,char *match_name,int avol,int kind,char *pure_path);
  43. long map_name_to_num(acache,match_name,avol,kind,pure_path)
  44. register cache_entry_pt acache;
  45. int avol;
  46. char *match_name;
  47. int kind;
  48. char *pure_path;
  49. {
  50.     HParamBlockRec cinfo;
  51.     char xxname[300];
  52.     int os;
  53.     int slen;
  54.     int csize=0;
  55.     while(acache->text[0]!=0) {
  56.         if(strcmp(match_name,&acache->text[1])==0)
  57.             return acache->num;
  58.         acache++;
  59.         csize++;
  60.     }
  61.     if(csize>CACHE_SIZE) {
  62.         printf("fatal - cash size exeeded, recompile protectomat with larger CACHE_SIZE",0);
  63.         fprintf(outfile,"fatal - cash size exeeded, recompile protectomat with larger CACHE_SIZE",0);
  64.         exit(1);
  65.     }
  66.  
  67.     /*
  68.      * not found so add it
  69.      */
  70.     slen=strlen(match_name);
  71.     memcpy(&acache->text[1],match_name,slen);
  72.     acache->text[0]=slen;
  73.     acache->text[slen+1]=0;
  74.     memset(&cinfo,0,sizeof(cinfo));
  75.     cinfo.objParam.ioObjType=kind;
  76.     cinfo.objParam.ioObjNamePtr=(ProcPtr)acache->text;
  77.     cinfo.objParam.ioVRefNum=avol;
  78.     memset(xxname,0,sizeof(xxname));
  79.     cinfo.objParam.ioNamePtr=(StringPtr)xxname;
  80.     os=PBHMapName(&cinfo,FALSE);
  81.     if(os!=0) {
  82.         printf("warning:get PBHMapName os err=%d on %s\n",os,pure_path);
  83.         fprintf(outfile,"warning:get PBHMapName os err=%d on %s\n",os,pure_path);
  84.         num_warnings++;
  85.         return BOGUS_ID;
  86.     }
  87.     if((acache->num=cinfo.objParam.ioObjID)==0) {
  88.         printf("warning:name '%s' on path '%s' does not exist\n",match_name,pure_path);
  89.         fprintf(outfile,"warning:name '%s' on path '%s' does not exist\n",match_name,pure_path);
  90.         num_warnings++;
  91.         return BOGUS_ID;
  92.     }
  93.     return acache->num;
  94. }
  95.  
  96. /*
  97.  * restore one directories protection
  98.  */
  99. void set_1_dir(char *fname,long avnum,long dir_id,long user_num,long group_num,long protection_num,char *pure_path);
  100. void set_1_dir(fname,avnum,dir_id,user_num,group_num,protection_num,pure_path)
  101. char *fname;
  102. long avnum;
  103. long dir_id;
  104. long user_num;
  105. long group_num;
  106. long protection_num;
  107. char *pure_path;
  108. {
  109.     int os;
  110.     HParamBlockRec cinfo;
  111.     int flen;
  112.     char aname[300];
  113.     char xxname[300];
  114.     memset(aname,0,sizeof(aname));
  115.  
  116.     flen=strlen(fname);
  117.     if(flen>250) {
  118.         printf("warning:user or group name too long '%s'\n",pure_path);
  119.         fprintf(outfile,"warning:user or group name too long '%s'\n",pure_path);
  120.         num_warnings++;
  121.         return;
  122.     }
  123.     xxname[1]=':';
  124.     strcpy(&xxname[2],fname);
  125.     xxname[0]=flen+1;
  126.  
  127.     memset(&cinfo,0,sizeof(cinfo));
  128.  
  129.     cinfo.accessParam.ioVRefNum=avnum;
  130.     cinfo.fileParam.ioDirID=dir_id;
  131.     cinfo.accessParam.ioNamePtr=(StringPtr)xxname;
  132.     cinfo.accessParam.ioACOwnerID=user_num;
  133.     cinfo.accessParam.ioACGroupID=group_num;
  134.     cinfo.accessParam.ioACAccess=protection_num;
  135.  
  136.     printf("user:%ld group:%ld prot:%lx - %s\n",user_num,group_num,protection_num,fname);
  137.     os=PBHSetDirAccess(&cinfo,FALSE);
  138.     if(os!=0) {
  139.         printf("warning:set access err=%d %s\n",os,pure_path);
  140.         fprintf(outfile,"warning:set access err=%d %s\n",os,pure_path);
  141.         num_warnings++;
  142.         return;
  143.     }
  144. }
  145.  
  146. /*
  147.  * parse a protection
  148.  */
  149. long parse_protection(char *protection);
  150. long parse_protection(char *protection)
  151. {
  152.     long result=0;
  153.     char ch;
  154.     char *scn=protection;
  155.     while((ch = (*scn++))!=0) {
  156.      long one_byte=0;
  157.      long shift;
  158.       switch(ch) {
  159.       case 'o':
  160.         shift=0;
  161.         break;
  162.       case 'g':
  163.         shift=8;
  164.         break;
  165.       case 'e':
  166.         shift=16;
  167.         break;
  168.       default:
  169.           goto barfo_protection;
  170.       }
  171.     if((*scn++)!='(')
  172.           goto barfo_protection;
  173.     while((ch = *scn++)!=')') {
  174.       switch(ch) {
  175.         case 0:
  176.             goto barfo_protection;
  177.         case 'r':
  178.           one_byte|=2;
  179.           break;
  180.         case 'w':
  181.           one_byte|=4;
  182.           break;
  183.         case 's':
  184.           one_byte|=1;
  185.           break;
  186.         default:
  187.             goto barfo_protection;
  188.         }
  189.       }
  190.      result|=(one_byte<<shift);
  191.     }
  192.  
  193.     return result;
  194.  
  195. barfo_protection:
  196.     printf("warning:bogus protection %s\n",protection);
  197.     fprintf(outfile,"warning:bogus protection %s\n",protection);
  198.     num_warnings++;
  199.     return BOGUS_PROTECTION;
  200. }
  201.  
  202. /*
  203.  * convert a vnum to text
  204.  */
  205. void parse_1_file(long avnum,long dir_id,char *user_name,char *group_name,char *protection,char *mac_path,char *pure_path);
  206. void parse_1_file(avnum,dir_id,user_name,group_name,protection,mac_path,pure_path)
  207. long avnum;
  208. long dir_id;
  209. char *user_name;
  210. char *group_name;
  211. char *protection;
  212. char *mac_path;
  213. char *pure_path;
  214. {
  215.     long user_num=map_name_to_num(user_cache,user_name,avnum,3,pure_path);
  216.     long group_num=map_name_to_num(group_cache,group_name,avnum,4,pure_path);
  217.     long protection_num=parse_protection(protection);
  218.     if(protection_num==BOGUS_PROTECTION)
  219.         return;
  220.     if((user_num==BOGUS_ID)||
  221.        (group_num==BOGUS_ID))
  222.            return;
  223.     set_1_dir(mac_path,avnum,dir_id,user_num,group_num,protection_num,pure_path);
  224. }
  225.  
  226. /*
  227.  * convert a vnum to text
  228.  */
  229. void read_input_file(void);
  230. void read_input_file()
  231. {
  232.     char apath[ERR_BUF_SIZE];
  233.     char pure_copy[ERR_BUF_SIZE];
  234.     char prev_vol[300];
  235.     long cur_vol_num=BAD_VOL_NUM;
  236.     char *s;
  237.     char *group_name;
  238.     char *user_name;
  239.     char *protection;
  240.     char *mac_path;
  241.     char *mac_vol;
  242.     char ch;
  243.     prev_vol[0]=0;
  244.     while(!read_line(apath,infile)) {
  245.         strcpy(pure_copy,apath);
  246.         s=apath;
  247.         ch= *s++;
  248.         if(ch==0)
  249.             continue;
  250.         if(ch==';')
  251.             continue;
  252.         if(ch != '=') {
  253.             printf("warning:bogus = '%s'\n",pure_copy);
  254.             fprintf(outfile,"warning:bogus = '%s'\n",pure_copy);
  255.             num_warnings++;
  256.             continue;
  257.         }
  258.         user_name=s;
  259.         group_name=strchr(user_name,',');
  260.         if(group_name==NIL) {
  261.             printf("warning:bogus group name '%s'\n",pure_copy);
  262.             fprintf(outfile,"warning:bogus group name '%s'\n",pure_copy);
  263.             num_warnings++;
  264.             continue;
  265.         }
  266.         *group_name++ =0;
  267.  
  268.         protection=strchr(group_name,',');
  269.         if(protection==NIL) {
  270.             printf("warning:bogus protection '%s'\n",pure_copy);
  271.             fprintf(outfile,"warning:bogus protection '%s'\n",pure_copy);
  272.             num_warnings++;
  273.             continue;
  274.         }
  275.         *protection++ =0;
  276.  
  277.         mac_vol=strchr(protection,',');
  278.         if(mac_vol==NIL) {
  279.             printf("warning:bogus volume name '%s'\n",pure_copy);
  280.             fprintf(outfile,"warning:bogus volume name '%s'\n",pure_copy);
  281.             num_warnings++;
  282.             continue;
  283.         }
  284.         *mac_vol++ =0;
  285.  
  286.         mac_path=strchr(mac_vol,':');
  287.         if(mac_path==NIL) {
  288.             printf("warning:bogus file name '%s'\n",pure_copy);
  289.             fprintf(outfile,"warning:bogus file name '%s'\n",pure_copy);
  290.             num_warnings++;
  291.             continue;
  292.         }
  293.         *mac_path++ =0;
  294.  
  295.         if(strcmp(mac_vol,prev_vol)!=0) {
  296.             strcpy(prev_vol,mac_vol);
  297.             cur_vol_num=get_vol_id(mac_vol);
  298.         }
  299.  
  300.         if(cur_vol_num==BAD_VOL_NUM)
  301.             continue;
  302.         parse_1_file(cur_vol_num,2L /*root*/ ,user_name,group_name,protection,mac_path,pure_copy);
  303.     }
  304. }
  305.  
  306. void open_files(void);
  307. void open_files()
  308. {
  309.     char apath[ERR_BUF_SIZE];
  310.     while(TRUE){
  311.       printf ("Enter file of protection to restore > ");
  312.       if(read_line(apath,stdin))
  313.           exit(1);
  314.       printf("opening '%s' for input\n",apath);
  315.       infile=fopen(apath,"r");
  316.       if(infile!=0)
  317.           break;
  318.       printf("can't open '%s' because errno=%d, reenter file name\n",apath,errno);
  319.     }
  320.     outfile=fopen(LOG_NAME,"w");
  321.     if(outfile==0) {
  322.         printf("fatal:can't open log file %s\n",LOG_NAME);
  323.         exit(1);
  324.     }
  325.     {
  326.         unsigned long now=time(0L);
  327.         fprintf(outfile,"Protectomat load of %s starting at %s",apath,ctime(&now));
  328.         printf("Protectomat load of %s starting at %s",apath,ctime(&now));
  329.     }
  330. }
  331.  
  332.  
  333. int main(argc,argv)
  334. int argc;
  335. char **argv;
  336. {
  337.     printf(";protectomat - load appleshare protections v%d.%d built on %s %s\n",
  338.         VER_MAJ,VER_MIN,__DATE__,__TIME__);
  339.     allocate_cache_entries();
  340.     open_files();
  341.     read_input_file();
  342.     fclose(infile);
  343.     {
  344.         unsigned long now=time(0L);
  345.         fprintf(outfile,"Protectomat load finished with %ld warnings at %s",num_warnings,ctime(&now));
  346.         printf("Protectomat load finished with %ld warnings at %s",num_warnings,ctime(&now));
  347.         fclose(outfile);
  348.     }
  349.     return 0;
  350. }
  351.